﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.AdvanceCommons;

namespace YT.XH.MF
{
    public partial class frmmInputMtn : Form
    {

        private DBAccess mdbcon = new DBAccess(false, false);
        public frmmInputMtn()
        {
            InitializeComponent();
        }

        private void FillData()
        {
            int intRow = 0;
            string strSQL = string.Empty;
            this.dgvDtl.Rows.Clear();
            strSQL = "select * from mInputHistory order by recid desc";
            DataTable dtDtl = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtDtl.Rows)
            {
                this.dgvDtl.Rows.Add();
                this.dgvDtl.Rows[intRow].Cells[ColSeqNo.Index].Value = CvtFunc.ToString(drRow["SeqNo"]);
                this.dgvDtl.Rows[intRow].Cells[ColUserName.Index].Value = CvtFunc.ToString(drRow["UserName"]);
                //this.dgvDtl.Rows[intRow].Cells[ColRemark.Index].Value = CvtFunc.ToString(drRow["Remark"]);
                this.dgvDtl.Rows[intRow].Cells[ColHospitalName.Index].Value = CvtFunc.ToString(drRow["HospitalName"]);
                this.dgvDtl.Rows[intRow].Cells[ColDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToString(drRow["InputDate"]), DateFormatOption.DateFmtYMD);
                intRow++;
            }

        }

        private void frmmInputMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
            mdbcon.CloseCon();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要导入上报数据？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    folderBrowserDialog1.ShowDialog();
                    string strPath = folderBrowserDialog1.SelectedPath;
                    if (strPath == "") return;
                    if (MessageBox.Show("请确保有足够的空间和权限进行本次操作. ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        mdbcon.BeginTran();
                        timer1.Start();
                        Cursor.Current = Cursors.WaitCursor;
                        if (InputData(strPath, mdbcon))
                        {
                            FillData();
                            mdbcon.CommitTran();
                            MessageBox.Show("操作完成!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            mdbcon.RollbackTran();
                        }
                        timer1.Stop();
                        Cursor.Current = Cursors.Default;
                        progressBar1.Visible = false;
                        progressBar1.Value = 0;
                        
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }


        private bool InputData(string strPath, DBAccess dbcon)
        {
            try
            {
                string strSQL = string.Empty;
                string strFields = string.Empty;
                lblInputDesc.Text = "";
                if (Directory.Exists(strPath))
                {

                    if (true)
                    {
                        DataSet dsDtl = new DataSet();
                        if (File.Exists(strPath + "\\mOutputTableName.xml"))
                        {
                            dsDtl.ReadXml(strPath + "\\mOutputTableName.xml");
                        }
                        else
                        {
                            strSQL = "select distinct TableName from mOutputTableName";
                            dsDtl = dbcon.GetDataTable(strSQL).DataSet;
                        }
                        DataTable dtTable = new DataTable();
                        dtTable = dsDtl.Tables[0];
                        progressBar1.Visible = true;
                        progressBar1.Value = 0;
                        progressBar1.Maximum = dtTable.Rows.Count + 1;
                        
                        foreach (DataRow drTable in dtTable.Rows)
                        {
                            this.progressBar1.Value++;
                            string strTableName = CvtFunc.ToString(drTable["TableName"]);
                            this.lblInputDesc.Text = "正在处理  " + strTableName +"...";          
                            
                            if (strTableName.ToUpper() == "mQryDetail".ToUpper()) continue;
                            if (strTableName.ToUpper() == "mQryHdr".ToUpper()) continue;
                            if (strTableName.ToUpper() == "mOutputTableName".ToUpper()) continue;
                            DataSet dsInputContent = new DataSet();
                            if (File.Exists(strPath + "\\" + strTableName + ".xml"))
                            {
                                dsInputContent.ReadXml(strPath + "\\" + strTableName + ".xml");
                                DataTable dtInputContent = new DataTable();
                                if (dsInputContent == null) continue;
                                if (dsInputContent.Tables.Count == 0) continue;
                                dtInputContent = dsInputContent.Tables[0];

                                strFields = DataFunctions.GetTableSQL(mdbcon, strTableName).ToUpper();
                                strFields = strFields.Replace("]", "");
                                strFields = strFields.Replace("[", "");



                                if (strFields == "") return false;




                                if (strTableName == "mOutputHistory")
                                {
                                    strFields = strFields.Replace(",FROMDATE,TODATE", "");
                                }
                                if (false ) // 只是更新加入新的，此种情况只先适用于前次上报数据填充比较完全的情况。
                                {
                                    foreach (DataRow dtInputCon in dtInputContent.Rows)
                                    {
                                        strSQL = GetSQLData(strTableName, strFields, dtInputCon);
                                        if (strSQL != "") dbcon.ExecuteSql(strSQL);
                                    }
                                }
                                else
                                {
                                    bool blnNeedDel = true;
                                    if (dtInputContent.Rows.Count > 0)
                                    {
                                        AnalysisFields(ref strFields, dtInputContent);


                                        if (strTableName == "mOutputHistory")
                                        {
                                            strSQL = "Delete " + strTableName + "";
                                            dbcon.ExecuteSql(strSQL);
                                            blnNeedDel = false;
                                        }
                                        else if ( strTableName.ToUpper ()=="titleResult".ToUpper ())
                                        {
                                            strSQL = "Delete " + strTableName + " where SickRecID='" + dtInputContent.Rows[0]["SickRecID"] + "'";
                                                dbcon.ExecuteSql(strSQL);
                                            blnNeedDel = false;
                                        }
                                        else if (strFields.IndexOf("SickRecID", StringComparison.InvariantCultureIgnoreCase) >= 0)
                                        {
                                            string strChkSickRecID = "";
                                            string strChkTableName = "";
                                            foreach (DataRow dtInputCon in dtInputContent.Rows)
                                            {
                                                if (strChkTableName == strTableName && strChkSickRecID == CvtFunc.ToString(dtInputCon["SickRecID"])) continue;
                                                if (strChkTableName == "" && strChkTableName == "")
                                                {
                                                    strChkSickRecID = CvtFunc.ToString(dtInputCon["SickRecID"]);
                                                    strChkTableName = strTableName;
                                                }

                                                strSQL = "Delete " + strTableName + " where SickRecID='" + CvtFunc.ToString(dtInputCon["SickRecID"]) + "'";
                                                dbcon.ExecuteSql(strSQL);
                                                blnNeedDel = false;
                                            }
                                        }
                                        foreach (DataRow dtInputCon in dtInputContent.Rows)
                                        {
                                            if (blnNeedDel)
                                            {
                                                strSQL = "Delete " + strTableName + " where SickRecID='" + dtInputCon["SickRecID"] + "'";
                                                dbcon.ExecuteSql(strSQL);
                                            }
                                            strSQL = GetSQLData_New(strTableName, strFields, dtInputCon);
                                            if (strSQL != "") dbcon.ExecuteSql(strSQL);
                                        }
                                    }
                                }
                            }
                        }
                        progressBar1.Visible = false ;
                        progressBar1.Value = 0;
                    }
                    lblInputDesc.Text = "";
                    if (File.Exists(strPath + "\\mOutputHistory.xml"))
                    {
                        DataSet dsHospital = new DataSet();
                        DataTable dtHospital = new DataTable();
                        dsHospital.ReadXml(strPath + "\\mOutputHistory.xml");
                        dtHospital = dsHospital.Tables[0];
                        if (dtHospital.Rows.Count >= 0)
                        {
                            string strHospitalID = CvtFunc.ToString(dtHospital.Rows[0]["HospitalID"]);
                            string strHospitalName = CvtFunc.ToString(dtHospital.Rows[0]["HospitalName"]);
                            string strSeqNo = DataFunctions.GetFieldValue("mInputHistory", "max(seqno) as SeqNo", "1=1");
                            strSeqNo = CvtFunc.ToString(CvtFunc.ToInt(strSeqNo) + 1);
                            strSQL = " Insert into mInputHistory(SeqNo,InputDate,UserName,HospitalID,HospitalName   )" +
                                            " Values ('" + strSeqNo + "', '" + BaseFunctions.FormatDate(DateTime.Now.Date.ToString(), DateFormatOption.DateFmtYMD) + "', " +
                                            " '" + SysVar.UserName + "'  , '" + strHospitalID + "','" + strHospitalName + "' " +
                                            " ) ";
                            mdbcon.ExecuteSql(strSQL);
                        }
                    }
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private void AnalysisFields(ref string strFields, DataTable dtInputContent)
        {
            string[] strTmp =null ;
            strTmp = strFields.Split( new char []{','}, StringSplitOptions.None);
            for (int i = 0; i <= strTmp.Length - 1; i++)
            {
                if (!dtInputContent.Columns.Contains(strTmp[i])) strFields = strFields.Replace("," + strTmp[i], "");
            }
        }

private string GetSQLData_New(string strTableName, string strFields, DataRow drRow)
        {
            string strSQL = "";
            ArrayList arrFieldsList = new ArrayList();
            arrFieldsList.AddRange(strFields.Split(','));
           
            strSQL = " INSERT INTO  " + strTableName + " ( " + strFields + " ) VALUES  ( ";
            string strValueSQL = string.Empty;
            foreach (string strFieldName in arrFieldsList)
            {
                if (!strFieldName.Contains("DATE"))
                    if (strFieldName != "BIRTH")
                        strValueSQL += "'" + CvtFunc.ToString ( drRow[strFieldName]).Replace("'","''")  + "',";
                    else
                        strValueSQL += "'" + CvtFunc.ToDateTime(drRow[strFieldName]) + "',";
                else
                    strValueSQL += "'" + CvtFunc.ToDateTime(drRow[strFieldName]) + "',";

            }
            if (strValueSQL == "") return "";
            strValueSQL = strValueSQL.Substring(0, strValueSQL.Length - 1);
            strSQL += strValueSQL + " ) ";
            return strSQL;
        }

        private string GetSQLData(string strTableName,string strFields, DataRow drRow)
        {
           
            string strSQL = "select * from " + strTableName + "";
            ArrayList arrFieldsList = new ArrayList();
            arrFieldsList.AddRange(strFields.Split(','));
           
            strSQL = " if not exists ( select * from " + strTableName + " where ";
            if (strTableName.ToUpper() == "EffectAfterTreateHdr".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'";
            }
            else if (strTableName.ToUpper() == "EffectAfterTreateResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' " +
                          " and DtlSeqNo ='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo ='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "ExamBeforeTreatHdr".ToUpper())
            {
                strSQL += " SickRecID ='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "ExamBeforTreatResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' " +
                          " and DtlSeqNo ='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo ='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'  ";
            }
            else if (strTableName.ToUpper() == "JocobsonResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' " +
                          " and DtlSeqNo ='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo ='" + CvtFunc.ToString(drRow["RowNo"]) + "' and FromModule='" + CvtFunc.ToString(drRow["FromModule"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'";
            }
            else if (strTableName.ToUpper() == "mContraindication".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ContraindicationType='" + CvtFunc.ToString(drRow["ContraindicationType"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'";
            }
            else if (strTableName.ToUpper() == "mContraindicationDetail".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and HdrContraindication='" + CvtFunc.ToString(drRow["HdrContraindication"]) + "' and DtlContraindication='" + CvtFunc.ToString(drRow["DtlContraindication"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'";
            }
            else if (strTableName.ToUpper() == "mOutputHistory".ToUpper())
            {
                strSQL += " HospitalID='" + CvtFunc.ToString(drRow["HospitalID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' ";
            }
            else if (strTableName.ToUpper() == "mSickperson".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' ";
            }
            else if (strTableName.ToUpper() == "mTreatHistory".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and TreatCaseName='"+ CvtFunc.ToString ( drRow["TreatCaseName"]) +"' and BeginDate='" + CvtFunc.ToDateString( drRow["BeginDate"])  +"'";
            }
            else if (strTableName.ToUpper() == "NoseCAPExamDetail".ToUpper())
            {
                strSQL +=" SickRecID='"+ CvtFunc.ToString(drRow["SickRecID"]) +"' and ExamSeqNo='"+ CvtFunc.ToString ( drRow["ExamSeqNo"] ) +"' " +
                    " and HdrSeqNo='"+ CvtFunc.ToString ( drRow["HdrSeqNo"])  +"' and DtlSeqNo='"+ CvtFunc.ToString(drRow["DtlSeqNo"]) +"' and RowNo='"+ CvtFunc.ToString ( drRow["RowNo"])  +"' and Type='"+ CvtFunc.ToString ( drRow["Type"])  +"'";
            }
            else if (strTableName.ToUpper() == "NoseCAPExamHdr".ToUpper())
            {
                strSQL += " SickRecID='"+ CvtFunc.ToString ( drRow["SickRecID"])  +"' and  SeqNo='"+ CvtFunc.ToString (drRow["SeqNo"])  +"' and ExamSeqNo='"+ CvtFunc.ToString (drRow["ExamSeqNo"])  +"' ";
            }
            else if ( strTableName.ToUpper ()=="NoseDrTreatHistoryDtl".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and  SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' ";
            }
            else if (strTableName.ToUpper() == "NoseLabExamHdr".ToUpper())
            {
                strSQL += " SickRecID='"+ CvtFunc.ToString ( drRow["SickRecID"])  +"' and SeqNo='"+ CvtFunc.ToString ( drRow["SeqNo"])  +"' and LabExamSeqNo='"+ CvtFunc.ToString ( drRow["LabExamSeqNo"])  +"'";
            }
            else if (strTableName.ToUpper() == "NoseLabExamDtl".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and HdrSeqNo ='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "'";
            }
            else if (strTableName.ToUpper() == "NoseNormalVisitRecordDtl".ToUpper())
            {
                strSQL +=" SickRecID='"+ CvtFunc.ToString(drRow["SickRecID"]) +"' and SeqNo='"+ CvtFunc.ToString ( drRow["SeqNo"])  +"'";
            }
            else if (strTableName.ToUpper() == "NoseSkinPrickTestHdr".ToUpper())
            {
                strSQL +=" SickRecID='"+ CvtFunc.ToString ( drRow["SickRecID"]) +"' and SeqNo='"+ CvtFunc.ToString (drRow["SeqNo"])  +"'";
            }
            else if (strTableName.ToUpper() == "NoseSkinPrickTestDetail".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' " +
                       " and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' and DtlSeqNo='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'";
            }
            else if (strTableName.ToUpper() == "NoseTreatmentHistoryDtl".ToUpper())
            {
                strSQL += " SickRecID='"+ CvtFunc.ToString (drRow ["SickRecID"]) +"' and InjectSeqNo='"+ CvtFunc.ToString (drRow ["InjectSeqNo"]) +"' and SeqNo='"+ CvtFunc.ToString (drRow["SeqNo"])  +"'";
            }
            else if (strTableName.ToUpper() == "NoseVisitRecordDtl".ToUpper())
            {
                strSQL +=" SickRecID='"+ CvtFunc.ToString ( drRow["SickRecID"])  +"' and SeqNo='"+ CvtFunc.ToString( drRow["SeqNo"])   +"'";
            }
            else if (strTableName.ToUpper() == "THIResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' " +
                          " and DtlSeqNo ='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo ='" + CvtFunc.ToString(drRow["RowNo"]) + "' and FromModule='" + CvtFunc.ToString(drRow["FromModule"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'";
            }
            else if (strTableName.ToUpper() == "NoseVisitWeek".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "'";
            }
            else if (strTableName.ToUpper() == "titleResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and HdrSeqNo ='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' and  DtlSeqNo='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectHdr".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' and DtlSeqNo='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectHdr_Emergency".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectResult_Emergency".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' and DtlSeqNo='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectHdr_Watch".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectResult_Watch".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' and DtlSeqNo='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectHdr_Sure".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and SeqNo='" + CvtFunc.ToString(drRow["SeqNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "TreatmentEffectResult_Sure".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' and DtlSeqNo='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "' ";
            }
            else if (strTableName.ToUpper() == "UntowardEffectResult".ToUpper())
            {
                strSQL += " SickRecID='" + CvtFunc.ToString(drRow["SickRecID"]) + "' and ExamSeqNo='" + CvtFunc.ToString(drRow["ExamSeqNo"]) + "' and HdrSeqNo='" + CvtFunc.ToString(drRow["HdrSeqNo"]) + "' " +
                          " and DtlSeqNo ='" + CvtFunc.ToString(drRow["DtlSeqNo"]) + "' and RowNo ='" + CvtFunc.ToString(drRow["RowNo"]) + "' and Type='" + CvtFunc.ToString(drRow["Type"]) + "'  ";
            }
            else
            {
                return "";
            }
            strSQL += " ) ";
            strSQL += " INSERT INTO  " + strTableName + " ( " + strFields + " ) VALUES  ( ";
            string strValueSQL = string.Empty;
            foreach (string strFieldName in arrFieldsList)
            {
                if (!strFieldName.Contains("DATE")  )
                    if ( strFieldName!="BIRTH")
                        strValueSQL += "'" + drRow[strFieldName] + "',";
                    else
                        strValueSQL += "'" + CvtFunc.ToDateTime(drRow[strFieldName]) + "',";
                else 
                    strValueSQL += "'" + CvtFunc.ToDateTime(  drRow[strFieldName]) + "',";
    
            }
            if (strValueSQL == "") return "";
            strValueSQL = strValueSQL.Substring(0, strValueSQL.Length - 1);
            strSQL += strValueSQL + " ) ";
            return strSQL;
        }

        private void frmmInputMtn_Load(object sender, EventArgs e)
        {
            this.Text = "数据导入";
            FillData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            progressBar1.Visible = true;
            this.Refresh();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }

        


    }
}
